package system;

import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JTable;

import ui.MainMenuUI;
import ui.ResultsUI;

public class Algorithm implements Runnable {

	
	/**
	 * stop any algorithm
	 */
	public static double[][] tempX;
	public static double temp;
	public static int num_of_docs;
	public static int clusters;
	public static ArrayList<double[]> col_sum;
	public static int index;
	public static ResultsUI result=null;
	public static double bestRandIndexValue=0;
	public static double[][] bestRandMatrix=null;
	private static double max;
	
	
	public static void run(int num_of_clusters,int num_of_documents,ArrayList <String> pathes)
	{
		
		StopWordsRemoval swr=new StopWordsRemoval();
		Stemmer stm = new Stemmer();
		BagOfWords bow=new BagOfWords();
		
		MainMenuUI.button_2.setEnabled(false);
	    MainMenuUI.X.button_4.setEnabled(false);
		///////////////////////////
		
		/*
		pathes=new ArrayList<String>();
		pathes.add("book1.txt");
		pathes.add("book2.txt");
		*/
		///////////////////////////
		
	///	MainMenuUI.lblRunStatus.setText("NMF Algorithm Started");
		
		RandIndex ri= new RandIndex();
	
	if(MainMenuUI.X.alg==1){//nmf algorithm
		/*	Stop words Removal--> Steemer --> Bag of Words --> NMF --> Rand Index	*/
		
		tempX=bow.run(stm.run(swr.run(pathes)),num_of_documents);	
		NmfAlg nmf= new NmfAlg(bow.m, bow.n,num_of_clusters,1000,0.01);
		ri.run(nmf.run(tempX), num_of_documents, num_of_clusters, bow.n);
		MainMenuUI.button_3.setEnabled(false);
	}//if selection of algorithm
	
	else{//model selection algorithm
        double[][] x;
        temp=0;
        bestRandIndexValue=0;
        tempX=bow.run(stm.run(swr.run(pathes)),num_of_documents);
        int num_to_colapse=(bow.m*bow.n*Integer.parseInt((MainMenuUI.X.txtpr.getText()))/100);
        ArrayList<double[][]> v_matrixes= new ArrayList<double[][]>();
        ArrayList<Double> d= new ArrayList<Double>();
        col_sum= new ArrayList<double[]> ();
        
        //Collapse x matrix
        for(int j=2;j<=10;j++){
        	x=tempX;
        		for(int i=0;i<num_to_colapse;i++){
        			int indi=(int)Math.abs(Math.random()*bow.m);
        			int indj=(int)Math.abs(Math.random()*bow.n);
        			x[indi][indj]=0.0;
        		}//for2
        
        MainMenuUI.lblRunStatus.setText("MODEL SELECTION IN PROGRESS...");
        MainMenuUI.progressBar.setValue((j/10)*100);
        NmfAlg nmf= new NmfAlg(bow.m, bow.n,j,1000,0.01);
        double[][] t=nmf.run(x);

        v_matrixes.add(t);
        d.add(ri.run(t, num_of_documents, j, bow.n));
        double[] col=RandIndex.colSum;
        		col_sum.add(col);
      }//for1
	
        max=0.0;
        index=0;
       	for(int i=0;i<8;i++){
		if(max<d.get(i)){
			max=d.get(i);
			index=i;
		}
	}
        bestRandIndexValue=max;
       	clusters=index+2;
       	NmfAlg.log=NmfAlg.log+("//////////////////////////////////////////////////////\n");
       	NmfAlg.log=NmfAlg.log+("Best Rand index value is: "+max+"\n\n");
       	NmfAlg.log=NmfAlg.log+("Best Classification matrix: \n");
       	for(int l=0;l<bow.n;l++){
	       for(int k=0;k<index+2;k++){
		      NmfAlg.log=NmfAlg.log+(l+".   ");
		    		  NmfAlg.log=NmfAlg.log+(new DecimalFormat("##.###").format((v_matrixes.get(index)[l][k])));
		    		  NmfAlg.log=NmfAlg.log+("\t\t ");
	       }
	       NmfAlg.log=NmfAlg.log+("\n");
         }
       
       NmfAlg.log=NmfAlg.log+("Best clusters number is: "+(index+2)+"\n");
       NmfAlg.log=NmfAlg.log+("//////////////////////////////////////////////////////\n");
       MainMenuUI.progressBar.setValue(100);
       MainMenuUI.lblRunStatus.setText("MODEL SELECTION COMPLETED!");
 
}//else
	
  
	MainMenuUI.button_2.setEnabled(true);
    MainMenuUI.X.button_4.setEnabled(true);
    bestRandIndexValue=max;
	result=new ResultsUI();
	result.setVisible(true);
	MainMenuUI.btnResults.setEnabled(true);
	MainMenuUI.button_3.setEnabled(false);
	}//run
	
	public void run()
	{
		if(MainMenuUI.alg==1)//nmf alg
		run(Integer.parseInt(MainMenuUI.textField.getText()),Integer.parseInt(MainMenuUI.textField_2.getText()),MainMenuUI.pathes);
		else//model selection alg
			run(0,Integer.parseInt(MainMenuUI.textField_2.getText()),MainMenuUI.pathes);
		MainMenuUI.progressBar.setIndeterminate(false);
		
	}
	
	
	
}

